namespace TheMethod;

public class Method
{
    static void Main(string[] args)
    {
    }
    /// <summary>
    /// 利用最小二乘法预测GDP
    /// </summary>
    /// <param name="problem">数据组</param>
    /// <param name="year">所预测年份</param>
    /// <returns></returns>

        public static double Methods(Points[] problem, int year)
    {  //计算x，y的平均值
        double avex = 0, avey = 0;
        double a, b;
        foreach (Points m in problem)
        {
            avex += m.X;
            avey += m.Y;
        }
        avex /= problem.Length;
        avey /= problem.Length;
        //用公式计算系数的分子和分母
        double numerator = 0;
        double denpminator = 0;
        foreach (Points m in problem)
        {
            numerator += (m.X - avex) * (m.Y - avey);
            denpminator += (m.X - avex) * (m.X - avex);
        }
        b = numerator / denpminator;
        a = avey - b * avex;
        return a + b * year;
    }
}
